import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author nhsoft.lul
 */
public class Application {
    private static HikariDataSource dataSource;
    private static final String DB_CONFIG_FILE = "jdbc.properties";

    static {
        //读取jdbc配置文件
        Properties props = new Properties();
        try {
            String realPath = Application.class.getClassLoader().getResource("")
                    .getFile();
            InputStream in = new FileInputStream(new File(realPath + DB_CONFIG_FILE));
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //初始化HikariConfig配置
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(props.getProperty("jdbcUrl"));
        config.setUsername(props.getProperty("username"));
        config.setPassword(props.getProperty("password"));
        config.addDataSourceProperty("cachePrepStmts", props.getProperty("dataSource.cachePrepStmts"));
        config.addDataSourceProperty("prepStmtCacheSize", props.getProperty("dataSource.prepStmtCacheSize"));
        config.addDataSourceProperty("prepStmtCacheSqlLimit", props.getProperty("dataSource.prepStmtCacheSqlLimit"));
        config.addDataSourceProperty("useServerPrepStmts", props.getProperty("dataSource.useServerPrepStmts"));
        config.addDataSourceProperty("useLocalSessionState", props.getProperty("dataSource.useLocalSessionState"));
        config.addDataSourceProperty("rewriteBatchedStatements", props.getProperty("dataSource.rewriteBatchedStatements"));
        config.addDataSourceProperty("cacheResultSetMetadata", props.getProperty("dataSource.cacheResultSetMetadata"));
        config.addDataSourceProperty("cacheServerConfiguration", props.getProperty("dataSource.cacheServerConfiguration"));
        config.addDataSourceProperty("elideSetAutoCommits", props.getProperty("dataSource.elideSetAutoCommits"));
        config.addDataSourceProperty("maintainTimeStats", props.getProperty("dataSource.maintainTimeStats"));
        //初始化HikariDataSource
        dataSource = new HikariDataSource(config);
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = Application.getConnection();

        String sql = "drop table if exists student";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.execute();

        sql = "create table student (id int primary key,name varchar(20))";
        statement = connection.prepareStatement(sql);
        statement.execute();

        sql = "insert into student(id,name) values(?, ?)";
        statement = connection.prepareStatement(sql);
        for (int i = 0; i < 50; i++) {
            statement.setInt(1, (i + 1));
            statement.setString(2, "lul" + i);
            statement.addBatch();
        }
        statement.executeBatch();
    }
}
